package com.chenshu.intercept_demo.interceptors;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;

/**
 * Created with IntelliJ IDEA
 * Description
 * User：chenshu
 * Date：2024-04-22
 * Time：22:18
 */
@ResponseBody
@ControllerAdvice
public class ExceptionAdvice {
    @ExceptionHandler(Exception.class)
    public HashMap<String, Object> ExceptionHandler(Exception e) {
        HashMap<String, Object> retMap = new HashMap<>();
        retMap.put("code", "-1");
        retMap.put("msg", e.getMessage());
        return retMap;
    }

    @ExceptionHandler(ArithmeticException.class)
    public HashMap<String, Object> ArithmeticExceptionHandler(ArithmeticException e) {
        HashMap<String, Object> retMap = new HashMap<>();
        retMap.put("code", "-2");
        retMap.put("msg", e.getMessage());
        return retMap;
    }
}
